package com.example.superoa.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.model.pojos.Titem;
import com.example.superoa.model.service.TitemsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Controller
	public class TitemsController {
			@Autowired
			private TitemsService itemservice;
			
			
			//查询所有		
			@PostMapping("select-items")
			@ResponseBody
			public Map<String,Object> show(String titemremark,int page,int rows) {
				Map<String,Object> map = new HashMap<>();
				//先分页
				Page<Object> pg = PageHelper.startPage(page, rows);
				JSONArray j = itemservice.findByitemname(titemremark);
				int i=1;
				for (Object object : j) {
					JSONObject obj=(JSONObject)object;
					obj.put("item", i);
					i++;
				}
				//总记录
				map.put("total", pg.getTotal());
				//页内容
				map.put("rows", j);
				return map;
			}
			/**新增
			 * @return
			 */
			@RequestMapping("select-testitems")
			@ResponseBody
			public String itmeinsert(Titem items) {
				itemservice.insertitems(items);
				return "redirect:main-administrator:testitems";				
			}
			//根据id查看
			@RequestMapping("item-xiangq")
			@ResponseBody
			public JSONArray itemxiangq(int titemid) {
				return itemservice.findByIditem(titemid);
			}
			//初始化
			@RequestMapping("items-all")
			@ResponseBody
			public JSONArray selectalls(int titemid) {		
				JSONArray json = itemservice.findByIditem(titemid);
				return json;
			}
			//修改题库
			@RequestMapping("items-xiugai")
			@ResponseBody
			public void xiugaiitem(int titemid,String titemremark,String titemanswer,String titemtypes, Date titemdate,String titemall) {
				System.err.println("接收到"+titemid+".."+titemremark+".."+titemanswer+".."+titemtypes+".."+titemdate+".."+titemall);
				itemservice.xiugaiitem(titemid, titemremark, titemanswer, titemtypes, titemdate, titemall);
			}
}